﻿//-----------------------------------------------------------------------
// <copyright file="GenerateKey.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr</email>
// <date>2004-03-23</date>
// <summary></summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Tools.StrongName
{
    using System;
    using System.IO;
    using System.Xml;
    using System.Globalization;
    using Microsoft.Win32;
    using Microsoft.Build.Framework;
    using Microsoft.Build.Tasks;
    using Microsoft.Build.Utilities;


    public sealed class GenerateKey : ToolTaskBase
    {
        /// <summary>
        /// Initializes a new instance of the GenerateKey class.
        /// </summary>
        public GenerateKey()
        {
        }

        /// <summary>The path to the snk file to generate</summary>
        [Required]
        public string DestinationKeyFile
        {
            get { return (string)Bag["DestinationKeyFile"]; }
            set { Bag["DestinationKeyFile"] = value; }
        }

        /// <summary>The runtime version to use for the signing i.e. 1.0, 1.1 or 2.0 </summary>
        [Required]
        public string Runtime
        {
            get { return (string)Bag["Runtime"]; }
            set { Bag["Runtime"] = value; }
        }       

        /// <summary>
        /// The name of the exe we call.
        /// </summary>
        /// <value>The name of the exe we call.</value>
        protected override string ToolName
        {
            get { return "Sn.exe"; }
        }

        /// <summary>
        /// Full calculated path to tool including exe name
        /// </summary>
        /// <returns>Defaults to default install location of Sn.exe</returns>
        protected override string GenerateFullPathToTool()
        {
            string pathToTool = Path.Combine(Microsoft.Sdc.Tasks.Configuration.Utilities.GetPathToSDK(this.Runtime), @"bin\" + this.ToolName);

            if (pathToTool == null)
            {
                Log.LogError("StrongName.NotFound");
            }
            return pathToTool;
        }     

        /// <summary>
        /// Builds the commandline for this tool
        /// </summary>
        /// <param name="commandLine">The object to append the args to.</param>
        /// <returns>True if successfully added args.</returns>
        protected override void AddCommandLineCommands(CommandLineBuilderExtension commandLine)
        {
            commandLine.AppendSwitch("-q");
            commandLine.AppendSwitch("-k");
            commandLine.AppendFileNameIfNotNull(this.DestinationKeyFile);
        }        
    }
}